const mongoose = require('mongoose');

const documentSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
    trim: true
  },
  fileName: {
    type: String,
    required: true
  },
  fileType: {
    type: String,
    required: true,
    enum: ['pdf', 'docx', 'txt', 'html']
  },
  filePath: {
    type: String,
    required: true
  },
  fileSize: {
    type: Number,
    default: 0
  },
  uploadedBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  chapters: [{
    title: String,
    startIndex: Number,
    endIndex: Number
  }],
  // 存储文档内容，用于断点续传
  content: {
    type: String,
    select: false // 默认不返回，节省带宽
  },
  processed: {
    type: Boolean,
    default: false
  },
  processingStatus: {
    type: String,
    enum: ['pending', 'processing', 'completed', 'failed'],
    default: 'pending'
  },
  processingError: {
    type: String,
    default: null
  },
  // 存储处理进度，用于断点续传
  processingProgress: {
    processedChunks: {
      type: Number,
      default: 0
    },
    totalChunks: {
      type: Number,
      default: 0
    },
    currentChapter: {
      type: Number,
      default: 0
    },
    lastProcessedAt: {
      type: Date,
      default: Date.now
    }
  },
  cardCount: {
    type: Number,
    default: 0
  },
  summary: {
    type: String,
    default: null
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

// 更新时间中间件
documentSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

const Document = mongoose.model('Document', documentSchema);

module.exports = Document; 